<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { propApi } from '@/api/controller';
  import { productTypeArr } from '@/pinia/getter';
  import { ToastSuccess, Toastloading, allNumber } from '@/utils/public';
  import xmUpload from '@/components/Upload/xm-upload.vue';
  const formLabelWidth = '100px'
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    title:'',
    dialogFormVisible:false,
    disabled:false,
    goodsClass:[] as Array<any>
  })
  interface formQuert {
    goods_id:number
    goods_type:number
    goods_name:string
    mall_goods_class:number
    is_show:number
    sort:number
    goods_desc:string
    big_img:string
    animate_resource:string
    animate_resource_md5:string
    is_new_mount:number
    text_color:string
    give_data_id:number
    animate_resource_small:string
  }
  /**
   * 旧需求中接口必传字段
   * is_new_mount 是否是新进场特效
   * text_color 进场特效进场条字体颜色值
   * give_data_id 情侣商品
   * animate_resource_small 名牌小图
   */
  const initialForm = {
    is_new_mount:0,
    text_color:'FFFFFF',
    give_data_id: 0,
    animate_resource_small:''
  }
  let form = reactive(initialForm as formQuert)
  const formRules = {
    goods_name: [{ required: true, message: "请输入商品名称", trigger: "blur" }],
    big_img: [{ required: true, message: "请上传图片", trigger: "blur" }],
    goods_type: [{ required: true, message: "请选择商品类型", trigger: "blur" }],
    is_show: [{ required: true, message: "请选择商品是否显示", trigger: "blur" }],
    mall_goods_class: [{ required: true, message: "请上传商品图片", trigger: "blur" }],
    animate_resource: [{ required: true, message: "请上传商品动画", trigger: "blur" }],
  }
  const handleSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      let submitSet = false
      const { goods_id } = form
      const loading = Toastloading()
      if(!goods_id) {
        await propApi.addGood(form).then(()=>{
          submitSet = true
          ToastSuccess('新增成功')
        }).catch(()=>{
          loading.close()
        })
      }else {
        await propApi.updateGood(goods_id,form).then(()=>{
          submitSet = true
          ToastSuccess('编辑成功')
        }).catch(()=>{
          loading.close()
        })
      }
      if(submitSet) {
        loading.close()
        closeDialog()
        $emit('updateList')
      }
    })
  }
  const openDialog = async(query:any = {})=> {
    const { 
      goods_id=0, goods_name='', goodsClass=[],
      goods_type='', mall_goods_class='', is_show='',
      sort='', goods_desc='', big_img='', animate_resource='',
      animate_resource_md5='', is_new_mount=0, text_color='FFFFFF',
      give_data_id=0, animate_resource_small='',
    } = query
    Object.assign(form,{
      goods_id, goods_name, goods_type,
      mall_goods_class, is_show,
      sort, goods_desc, big_img, animate_resource,
      animate_resource_md5, is_new_mount, text_color,
      give_data_id, animate_resource_small,
    })
    action.goodsClass = goodsClass
    action.title = `${goods_id?'编辑商品：':'新增商品'}${goods_name}`
    action.dialogFormVisible = true
  }
  const closeDialog = ()=>{
    form = reactive(initialForm as formQuert)
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
      top="10vh"
      class="xm-dialog input-width"
      v-model="action.dialogFormVisible"
      :close-on-click-modal="action.disabled"
      :show-close="action.disabled"
      :close-on-press-escape="action.disabled"
      width="60%"
      :title="action.title">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <el-row>
          <el-col :span="12">
            <el-form-item :label-width="formLabelWidth" label="商品名称" prop="goods_name">
              <el-input v-model="form.goods_name" placeholder="请输入商品名称"  />
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" label="是否显示" prop="is_show">
              <el-select v-model="form.is_show" placeholder="请选择是否显示商品">
                <el-option label="显示商品" :value="1" />
                <el-option label="隐藏商品" :value="0" />
              </el-select>
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" label="商品排序" prop="sort">
              <el-input v-model="form.sort" @input="allNumber(form,'sort')" placeholder="请输入商品排序" />
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" label="商品图片" prop="big_img">
              <xmUpload
                v-model:file-url="form.big_img"
                :data="{
                  md5:1,
                  type:7,
                }"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label-width="formLabelWidth" label="商品类型" prop="goods_type">
              <el-select v-model="form.goods_type" placeholder="请选择商品类型">
                <el-option
                  v-for="item in productTypeArr"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" label="商品分类" prop="mall_goods_class">
              <el-select v-model="form.mall_goods_class" placeholder="请选择商品分类">
                <el-option
                  v-for="item in action.goodsClass"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" label="商品描述" prop="goods_desc">
              <el-input
                v-model="form.goods_desc"
                :autosize="{ minRows: 3, maxRows: 3}"
                type="textarea"
                placeholder="请输入商品描述"
              />
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" label="商品动画" prop="animate_resource">
              <xmUpload
                list-type="text"
                v-model:file-url="form.animate_resource"
                v-model:file-md5="form.animate_resource_md5"
                :data="{
                  md5:2,
                  type:7,
                }"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>